//
//  PBAccountDetailCell.m
//  PBHomeDemo
//
//  Created by Playboy on 2018/5/30.
//  Copyright © 2018年 tmkj. All rights reserved.
//

#import "PBAccountDetailCell.h"

@interface PBAccountDetailCell ()
@property (weak, nonatomic) IBOutlet UILabel *numLabel;
@property (weak, nonatomic) IBOutlet UIButton *stateButton;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
@property (nonatomic, strong) PBAccountListModel *model;
@property (nonatomic, strong) NSMutableArray    *dataSource;
@end

@implementation PBAccountDetailCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)refreshWithArray:(NSMutableArray *)array indexPath:(NSIndexPath *)indexPath isLife:(BOOL)isLife{
    self.dataSource = array;
    PBAccountListModel *model = array[indexPath.section];
    self.model = model;
    self.numLabel.text = [NSString stringWithFormat:@"账单编号:%@",model.ID];
    if (model.status == 1) {
        [self.stateButton setTitle:@"待支付" forState:UIControlStateNormal];
        self.stateButton.backgroundColor = UIColorWithRGB(249, 159, 13);
    }else if (model.status == 2) {
        [self.stateButton setTitle:@"已支付" forState:UIControlStateNormal];
        self.stateButton.backgroundColor = UIColorWithRGB(81,183,255);
    }else if (model.status == 3) {
        [self.stateButton setTitle:@"作废" forState:UIControlStateNormal];
        self.stateButton.backgroundColor = [UIColor redColor];
    }
    
    // 富文本
    NSString *price = [NSString stringWithFormat:@"¥%@",model.payable];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:price];
    [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, 1)];
    self.priceLabel.attributedText = attr;
    
    NSString *startTime = [self returndate:model.startTime];
    NSString *endTime = [self returndate:model.endTime];
    self.timeLabel.text = [NSString stringWithFormat:@"日期：%@-%@",startTime,endTime];
    if (isLife) {
        self.typeLabel.hidden = NO;
        self.typeLabel.text = model.name;
        self.topConstraint.constant = 5;
    }else {
        self.typeLabel.hidden = YES;
        self.topConstraint.constant = -5;
    }
    
    // 切换账单时清除选中状态 同时清除合计总额
    if (!model.isSelected) {
        self.selectButton.selected = NO;
    }
    
    // 如果全部没有选中，则清空总额
    NSInteger count = 0;
    for (NSInteger i = 0; i < array.count; i ++) {
        PBAccountListModel *list = array[i];
        if (list.isSelected) {
            count ++;
        }
    }
    if (count == 0) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(clearMoney)]) {
            [self.delegate clearMoney];
        }
    }
    
    
}

- (NSString *)returndate:(NSString *)num{
    NSString *str1=[NSString stringWithFormat:@"%@",num];
    int x=[[str1 substringToIndex:10] intValue];
    NSDate *date1 = (NSDate *)[NSDate dateWithTimeIntervalSince1970:x];
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
    [dateformatter setDateFormat:@"yyyy年MM月dd"];
    return [dateformatter stringFromDate:date1];
}


#pragma mark - click
- (IBAction)selectMethodClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        // 增加金额
        if (self.delegate && [self.delegate respondsToSelector:@selector(addMoney:)]) {
            [self.delegate addMoney:self.model.payable];
        }
        
        self.model.isSelected = YES;
        
        [self isAllSelected];
        
        
    }else {
        // 减少金额
        if (self.delegate && [self.delegate respondsToSelector:@selector(reduceMoney:)]) {
            [self.delegate reduceMoney:self.model.payable];
        }
        
        self.model.isSelected = NO;
        
        [self isAllSelected];
        
    }
}

- (void)isAllSelected {
    NSInteger count = 0;
    for (PBAccountListModel *model in self.dataSource) {
        if (model.isSelected) {
            count ++;
        }
    }
    if (count == self.dataSource.count) {
        // 全选
        if (self.delegate && [self.delegate respondsToSelector:@selector(allSelected)]) {
            [self.delegate allSelected];
        }
    }else {
        // 没全选
        if (self.delegate && [self.delegate respondsToSelector:@selector(notAllSelected)]) {
            [self.delegate notAllSelected];
        }
    }
}


@end
